'use strict';
angular.module('CcxiApp', []);
app.filter('textLengthSet', function () {
    return function (value, wordwise, max, tail) {
        if (!value) return '';

        max = parseInt(max, 10);
        if (!max) return value;
        if (value.length <= max) return value;

        value = value.substr(0, max);
        if (wordwise) {
            var lastspace = value.lastIndexOf(' ');
            if (lastspace != -1) {
                value = value.substr(0, lastspace);
            }
        }

        return value + (tail || ' …');//'...'可以换成其它文字
    };
});
app.controller('findAllUnreadCtrl', ['$scope', '$modalInstance', '$cookies', 'homeService', 'unreadResult', 'domain', function ($scope, $modalInstance, $cookies, homeService, unreadResult, domain) {
    $scope.unreadResult = unreadResult;
    $scope.app = {};
    $scope.app.domain = domain;
    $scope.confirm = {}
    $scope.ok = function () {
        $modalInstance.close();
        $scope.$parent.closeAll=true;
    };


    $scope.confirmNotice = function (id, createdTime) {
        $modalInstance.close();
        $scope.$parent.closeAll=true;
        $scope.confirm.sessionId = $cookies.sessionId;
        $scope.confirm.noticeId = id;
        $scope.confirm.currentRecord = createdTime;
        homeService.confirmNotice($scope).then(function (result) {

        });
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}])
    ;
app.controller('homeCtrl', ['$scope', '$cookies', '$modal', 'homeService','$rootScope',function ($scope, $cookies, $modal, homeService,$rootScope) {
    $scope.legend = ["付款金额", "下单数"];
    $scope.item = ['--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--'];
    $scope.data = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ];

    //关闭所有模态框
    $rootScope.closeAll=false;

    //查询店铺信息
    $scope.shop = null;

    //查询所有的未读消息参数
    $scope.noticeRead = {};
    //未读消息
    $scope.unreadResult = null;
    //取得所有的未读消息
    $scope.queryUnread = function () {
        $scope.noticeRead.sessionId = $cookies.sessionId;
        homeService.queryUnread($scope).then(function (result) {
            if(result !=null){
                $scope.unreadResult = result;
                if ($scope.unreadResult.list != undefined && $scope.unreadResult.list.length>0) {
                    $scope.open();
                }
            }
        });
    }
    //模态提示框
    var src = $scope.app.domain;
    $scope.open = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'findAllUnread.html',
            controller: 'findAllUnreadCtrl',
            size: size,
            resolve: {
                unreadResult: function () {
                    return $scope.unreadResult;
                },
                domain: function () {
                    return src;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            console.log('点击确认');
        }, function () {

        });
    };
    $scope.queryUnread();

    $scope.queryShop = function () {
        homeService.queryShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shop = result.data;
                    if($scope.shop.auditStatus==0 || $scope.shop.auditStatus==1 || ($scope.shop.auditStatus==2 && $scope.shop.status==1) || ($scope.shop.auditStatus==2 && $scope.shop.status==2)){
                        $scope.openPopup();
                    }
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryShop();


    //打开弹窗
    $scope.openPopup = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/popup/popup.html',
            controller: 'popupCtrl',
            size: 'md',
            backdrop:"static",
            scope:$scope,
            resolve: {
                data: function () {
                    return  $scope.shop;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

    $scope.ordersMonth = new Date().getMonth() + 1;
    $scope.ordersYears = new Date().getFullYear();
    $scope.months = [];
    $scope.years = [];

    var getYear = function () {
        for (var i = 2015; i < $scope.ordersYears; i++) {
            $scope.years.push(i + 1);
        }
    }

    var getMonth = function () {
        $scope.months = [];
        var nowYear = new Date().getFullYear();
        $scope.ordersMonth = ($scope.ordersYears == nowYear) ? (new Date().getMonth() + 1) : 12;
        for (var i = 0; i < $scope.ordersMonth; i++) {
            $scope.months.push(i + 1);
        }
    }

    getYear();
    getMonth();

    //查询统计信息
    $scope.queryOrdersInfo = function () {
        homeService.queryOrdersInfo($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.item = result.data.item;
                    $scope.data = result.data.data;
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryOrdersInfo();

    $scope.changeMonth = function () {
        $scope.queryOrdersInfo();
    }

    $scope.changeYear = function () {
        getMonth();
        $scope.queryOrdersInfo();
    }



    //查询统计信息
    $scope.header = {
        totalMoney: 0.00,
        totalCount: 0,
        todayMoney: 0.00,
        todayCount: 0

    }
    $scope.queryHomeHeader = function () {
        homeService.queryHomeHeader($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.header = result.data;
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryHomeHeader();

    //
    $scope.goodsMonth = new Date().getMonth() + 1;

    $scope.goodsMonths = [];
    var getNowMonth = function () {

        for (var i = 0; i < $scope.goodsMonth; i++) {
            $scope.goodsMonths.push(i + 1);
        }
    }

    getNowMonth();

    $scope.queryForGoods = function () {
        homeService.queryForGoods($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goods = result.data;
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryForGoods();

    $scope.changeGoodsMonth = function () {
        $scope.queryForGoods();
    }



    //
    $scope.queryInternalNotice = function () {
        homeService.queryInternalNotice($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.notices = result.data;
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryInternalNotice();
}]);


app.controller('popupCtrl', ['$scope', '$modalInstance','toaster','data','$state', function($scope, $modalInstance,toaster,data,$state) {
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        $scope.changeUrl=function(){
            $scope.cancel();
            $state.go("app.shopDetailInfo");
        }

        //父级closeAll变化关闭模态框
        $scope.closeAllChange=function (newValue,oldValue,scope){
            if(newValue) {
                $modalInstance.dismiss('cancel');
            }
        }
        $scope.$watch("$parent.closeAll",$scope.closeAllChange,false);
}]);


app.directive('line', function () {
    return {
        scope: {
            id: "@",
            legend: "=",
            item: "=",
            data: "="
        },
        restrict: 'E',
        template: '<div style="height:400px;width: 100%;"></div>',
        replace: true,
        link: function ($scope, element, attrs, controller) {
            var option = {
                // 提示框，鼠标悬浮交互时的信息提示
                color: ['#3388ff', '#F15755', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'],
                tooltip: {
                    trigger: 'axis',
                    textStyle: {
                        color: '#fff'
                    },
                    fontSize: 12,
                    padding: 15,
                    show: true,
                    formatter: function (params) {
                        var res = '';
                        var date = '';
                        var money = '';
                        var ordersCount = '';
                        for (var i = 0; i < params.length; i++) {
                            date = params[0].name + '</br>';
                            if (params[i].seriesIndex == 0) {
                                money = params[i].seriesName + ":￥" + params[i].data + '</br>';
                            } else if (params[i].seriesIndex == 1) {
                                ordersCount = params[i].seriesName + ":" + params[i].data + '单';
                            }
                        }
                        res = date + money + ordersCount;
                        return res
                    }
                },
                // 图例
                legend: {
                    data: $scope.legend
                },
                // 横轴坐标轴
                xAxis: [{
                    type: 'category',
                    splitLine: {
                        show: false
                    },
                    data: $scope.item,

                }],
                // 纵轴坐标轴
                yAxis: [{
                    axisLine: {
                        show: false
                    },
                    splitLine: {
                        show:false,
                        lineStyle: {
                            type: 'dotted'
                        }
                    },
                    type: 'value'
                }],
                // 数据内容数组
                series: function () {
                    var serie = [];
                    for (var i = 0; i < $scope.legend.length; i++) {
                        var item = {
                            name: $scope.legend[i],
                            type: 'line',
                            symbol: 'circle',
                            smooth: true,
                            data: $scope.data[i]
                        };
                        serie.push(item);
                    }
                    return serie;
                }()
            };
            var myChart = echarts.init(document.getElementById($scope.id), 'macarons');
            myChart.setOption(option);

            $scope.$watch('item', function () {
                option.xAxis = [{ type: 'category', data: $scope.item }];
                var serie = [];
                for (var i = 0; i < $scope.legend.length; i++) {
                    var item = {
                        name: $scope.legend[i],
                        type: 'line',
                        symbol: 'circle',
                        smooth: true,
                        data: $scope.data[i]
                    };
                    serie.push(item);
                }
                option.series = serie;
                myChart.clear();
                myChart.setOption(option, true);
            })
        }
    };
});